package com.southpark.demo.control.commands.config
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.southpark.demo.control.events.config.LoadThemeEvent;
	import com.southpark.demo.control.responders.BaseResponder;
	
	import flash.events.IEventDispatcher;
	
	import mx.events.StyleEvent;
	import mx.styles.StyleManager;


	public class LoadThemeCommand extends BaseResponder
	{
		private var eventDispatcher:IEventDispatcher;
		
		override public function execute(cairngormEvent:CairngormEvent):void
		{
			super.execute(cairngormEvent);
			var event:LoadThemeEvent = cairngormEvent as LoadThemeEvent;
			
			eventDispatcher = StyleManager.loadStyleDeclarations(event.source);
			eventDispatcher.addEventListener(StyleEvent.COMPLETE,onLoadingSuccess);
			eventDispatcher.addEventListener(StyleEvent.ERROR,onLoadingFault);
		}
		
		private function onLoadingSuccess(event:StyleEvent):void
		{
			eventDispatcher.removeEventListener(StyleEvent.COMPLETE,onLoadingSuccess);
			eventDispatcher.removeEventListener(StyleEvent.ERROR,onLoadingFault);
			super.onSuccess(null);
		}
		
		private function onLoadingFault(event:StyleEvent):void
		{
			eventDispatcher.removeEventListener(StyleEvent.COMPLETE,onLoadingSuccess);
			eventDispatcher.removeEventListener(StyleEvent.ERROR,onLoadingFault);
			super.onFault(null);
		}
		
	}
}